In C++, within scope, we can have more than one definition for a Function and operator, which mean using the same function and operator we can perform different tasks, though functions and operators are designed to perform a specific task, but using the concept of overloading we can switch between the definitions of functions and operators.
Types of Overloading
In C++ we can have two types of Overloading:
- Function Overloading
- Operator Overloading
Function Overloading
In Function Overloading, we have multiple functions with the same name in the same scope with different arguments. The definition of the function also differs from each other that the overall concept of function overloading. When we call the overloaded function, the compiler checks the return type and parameters of the Overloaded function and calls the appropriate function. Function overloading Notes
- The function's name and scope should be the same.
- The parameter of functions should be of different data types or in numbers.
- The body of the function could be different.
Function overloading Examples For normal user-defined functions:
#include <iostream> using namespace std; int add(int a, int b) // this function add retun the sum of two numbers { int sum =0; sum =a +b; cout<<"You have called the add function with 2 arguments and your result is: "; cout<<sum; cout<<endl;// to enter a new line } int add(int a, int b, int c) // this function add() return the sum of 3 numbers { int sum=0; sum= a+b+c; cout<<"You have called the add function with 3 arguments and your result is: "; cout<<sum; cout<<endl;// to enter a new line } int main() { // calling the function add() wiht two arguments add(10, 20); //calling the function add with 3 arguments add(10, 20, 30); return 0; }
Output
You have called the add function with 2 arguments and your result is: 30 You have called the add function with 3 arguments and your result is: 60
Behind the code In this example we have performed the function overloading concept, here we have two user-defined functions with the same name and different arguments, the function add(int a, int b) contains only two arguments and the function add(int a, int b, int c) contains 3 arguments. So when we call the add(10,20) function then it executes the add(int a, int b), and when we call the add(10, 20, 30); function it executes the add(int a, int b, int c) function. So by passing the different argument, we switch between two different functions having the same name. Here the scope of add(int a, int b) and add(int a, int b, int c) is also same, which mean they both are defined outside the main() function.
Function overloading in a Class:
Example
#include <iostream> using namespace std; class Overloading { public: void tell(int a) { cout<<"You have passed an Integer value: "<<a; cout<<endl; } void tell(char a) { cout<<"You have passed a Character value: "<<a; cout<<endl; } void tell(double a) { cout<<"You have passed a double value: "<<a; cout<<endl; } }; int main() { Overloading fun; //fun is a object of Overloading class fun.tell(10); fun.tell('a'); fun.tell(10.01); return 0; }
Output
You have passed an Integer value: 10 You have passed a Character value: a You have passed a double value: 10.01
Behind the code In this example, we have performed the function overloading inside the class. Here we have the multiple class member functions tell() with different arguments and definitions. The scope of all the tell() function is the same because they are in declared public inside the class.
Operator Overloading
Operators are the special symbols we use in C++ to perform an action, but with the help of Operator overloading, we can redefine the operation of some Operators. C++ gives us the ability to provide the operators with some special meaning for different data types, for instance, if we apply the + operator between two digits then it will perform the addition operation, but if we define the + operator between two characters it will perform the concatenation operation. To perform the operator overloading we create an operator overloading function that redefines the operator operations. Here overloading means we are redefining the task of the operator. Syntax to perform operator overloading function:
return_type operator operator_symbol(Class_Name object_name) { //what should the operator do }
return_type represents the return data type of the Operator overloading function. Class_Name we create the operator overloading function inside the class. operator is a keyword operator_symbol represent the operators which we want to redefine.
Rules to perform the Operator Overloading:
- Only some of the Existing Operator can be overloaded.
- The operator overloading works only on the user-defined class objects, we can not overload the operator for the built-in data type, for example, we cannot alter the definition of + operator for 2+3 or 2+6 because here 2, 3 and 6 are integers and built-in data types.
- We do not pass any explicit argument when we overload a unary operator (an operator which require only one operand to perform the operation such as ++, --, etc.)
- When we overload a binary operator (operator which require two operands to perform an operation such as +, -, * etc.) then we only pass the class Object data type as an explicit argument in the operator overload function.
Operator overloading Example:
#include <iostream> using namespace std; class Op_Over { private : int x; public: Op_Over(int initialize) //counstructor { x=initialize; } void operator+(Op_Over obj) //operator overloding function { int mul ; mul = x * obj.x; cout<<"You have been tricked here + operator perform * operation "<<mul; } }; int main() { Op_Over op1(10), op2(20); op1+op2; // using + operator between two user defined class objects. return 0; }
Output:
You have been tricked here + operator perform * operation 200
Behind the code: In this example using the void operator+(Op_Over obj) function we overload or redefine the + operator task, so when we use the + operator between two class objects op1+op2; then it invoked the void operator+(Op_Over obj) function. Here the op1 object used the op2 object as an argument for the +(Op_Over obj) parameter, so when the statement mul = x * obj.x; statement execute it state x = 10 and obj.x =20.
Overloadable and Non Overloadable operators
There are only a few operators which we cannot overload but most of the operators are overloadable.
Overloadable Operators:
+ | - | * | / | % | ^ |
& | | | ~ | ! | , | = |
< | > | <= | >= | ++ | -- |
<< | >> | == | != | && | || |
+= | -= | /= | %= | ^= | &= |
|= | *= | <<= | >>= | [] | () |
-> | ->* | new | new [] | delete | delete [] |
Non Overloadable operator
:: | .* | . | ?: |
Operator Overloading Examples
There are various types of Operators in C++ so please check out these examples for a better understanding of Operator Overloading. Sr.No Operators & Example 1 Unary Operators Overloading 2 Binary Operators Overloading 3 Relational Operators Overloading 4 Input/Output Operators Overloading 5 Assignment Operators Overloading 6 Function call () Operator Overloading
Overloading Quick Summary
- In function overloading, we have multiple functions with the same function name and different arguments.
- In function overloading the compiler decides which function to call according to the passed arguments.
- The overloaded functions must be defined in the same scope.
- In operator Overloading, we redefine the operator task.
- We cannot overload every operator.
- The operator overload only works for user-defined data type or data structure. for instance, we cannot change the + operator task for 2 + 2, because 2 is not a user-defined object, but if we create a class object which is a user-defined, and for them, we can redefine the operator definition.
People are also reading: